package laps.dataaccess.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import laps.dataaccess.RankLeaveDAO;

public class RankLeaveDAOImpl implements RankLeaveDAO {

	private Connection connection;

	public RankLeaveDAOImpl(Connection connection) {
		super();
		this.connection = connection;
	}

	@Override
	public int getAnnualLeaveDays(String rank) {
		return getLeaveDays(rank, "ANNUAL");
	}

	@Override
	public int getMedicalLeaveDays(String rank) {
		return getLeaveDays(rank, "MEDICAL");
	}
	
	private int getLeaveDays(String rank, String type) {
		int days = 0;
		
		String sqlStatement = "select days from laps_db.rank_leave where " +
							  "rank=" + "'" + rank + "'" +
							  "leave_type=" + "'" + type + "'" + ";";
		
		Statement statement = null;
		ResultSet resultSet = null;
		
		try
		{
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sqlStatement);
			
			resultSet.next();
			days = resultSet.getInt("days");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return days;
	}
	
	
}
